package com.keensense.vcm.internal;

import com.keensense.vcm.HttpMethod;
import com.keensense.vcm.common.auth.CredentialsProvider;
import com.keensense.vcm.common.comm.RequestMessage;
import com.keensense.vcm.common.comm.ServiceClient;
import com.keensense.vcm.common.parser.ResponseParsers;
import com.keensense.vcm.model.GetDataDictionaryRequest;
import com.keensense.vcm.model.GetDataDictionaryResponse;
import com.keensense.vcm.model.SearchAlarmResponse;
import com.keensense.vcm.model.enums.LanguageEnum;
import com.keensense.vcm.model.enums.TypeCodeEnum;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Optional;

/**
 * @author lanjian
 * @date 2020/8/26
 */
public class DataDictionaryOperation extends VCMOperation {
    public DataDictionaryOperation(ServiceClient client, CredentialsProvider credsProvider) {
        super(client, credsProvider);
    }

    /**
     * 获取数据字典
     *
     * @param language
     * @param typeCode
     * @return
     */
    public GetDataDictionaryResponse getDataDictionary(LanguageEnum language, TypeCodeEnum typeCode) {
        GetDataDictionaryRequest request = new GetDataDictionaryRequest(typeCode);
        String xml = getXmlFromObject(request, GetDataDictionaryRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage requestMessage = new VCMRequestMessageBuilder(getInnerClient()).setInputStream(in)
                .setInputSize(xml.getBytes().length).setEndpoint(endpoint).setMethod(HttpMethod.POST).build();
        String resourcePath = Optional.ofNullable(language).map(s -> VCMResourcePathConstants.GET_DATA_DICTIONARY + "?language=" + s.name())
                .orElse(VCMResourcePathConstants.GET_DATA_DICTIONARY + "?language=" + LanguageEnum.zh_CN);
        requestMessage.setResourcePath(resourcePath);
        GetDataDictionaryResponse result = doOperation(requestMessage, ResponseParsers.getDataDictionaryResponseParser, true);
        return result;
    }
}
